<?php

namespace catchAdmin\info\controller;

use catchAdmin\permissions\model\Users;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;

use http\Client\Request;
use think\facade\Db;

class Jmj extends CatchController
{
    public function index(CatchAuth $auth)
    {
//        p(request()->param());

        $user = $auth->user();

        $data = request()->param();
        $data['sndCategory'] = join(' ', $data['sndCategory']);
        $data['UUID'] = $this->generateUUID();
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['shenhe'] = 0;

        if (!empty($data['id'])){
            $data['creator_id'] = db::name('jmj')->find($data['id'])['creator_id'];
            $data['UUID']= db::name('jmj')->find($data['id'])['UUID'];
            $arr = [];
            $arr['nameInitials'] = $data['nameInitials'];
            $arr['category'] = $data['category'];
            $arr['sndCategory'] = $data['sndCategory'];
            $arr['dsc'] = $data['dsc'];
            $arr['poster'] = $data['poster'];
            $arr['posterH'] = $data['posterH'];
            $arr['definition'] = $data['definition'];
            $arr['lengthUnit'] = $data['lengthUnit'];
            $arr['area'] = $data['area'];
            $arr['year'] = $data['year'];
            $arr['language'] = $data['language'];
            $arr['director'] = $data['director'];
            $arr['directorInitials'] = $data['directorInitials'];
            $arr['screenWriter'] = $data['screenWriter'];
            $arr['starring'] = $data['starring'];
            $arr['starringInitials'] = $data['starringInitials'];
            $arr['premiereChannel'] = $data['premiereChannel'];
            $arr['premiereTime'] = $data['premiereTime'];

            $arr['source'] = $data['source'];
            $arr['vipFlag'] = $data['vipFlag'];
            $arr['contentProvider'] = $data['contentProvider'];
            $arr['playCertification'] = $data['playCertification'];
            $arr['shenhe'] = 0;
            db::name('jm')->where('field101' , $data['name'])->update($arr);
        } else {
            $data['creator_id'] = $user->id;
        }

        Db::name('jmj')->save($data);
        return CatchResponse::success(1, '提交成功', 10000);

    }

    public function getJmjList(CatchAuth $auth)
    {
        $user = $auth->user();
        $role = Db::name('user_has_roles')->where('uid', $user->id)->value('role_id');
        $page = request()->param();
        if ($role == 8) {
            $data = Db::name('jmj')->where('creator_id', $user->id)->order('id', 'desc')->wherelike('name',$page['name'])->paginate([
                'list_rows' => $page['size'],
                'page' => $page['page'],
            ])->toArray();
        } else {
            $data = Db::name('jmj')->wherelike('name',$page['name'])->order('id', 'desc')->paginate([
                'list_rows' => $page['size'],
                'page' => $page['page'],
            ])->toArray();
        }
        foreach ($data['data'] as &$v) {
            $v['a5'] =   $auth->user()->id;
        }
        return CatchResponse::success($data, '提交成功', 10000);
    }


    public function getlist(CatchAuth $auth)
    {
        $user = $auth->user();
        $role = Db::name('user_has_roles')->where('uid', $user->id)->value('role_id');
        if ($role == 8) {
            $data = Db::name('jmj')->where('creator_id', $user->id)->order('id','desc')->column('id as value,name as label');
        } else {
            $data = Db::name('jmj')->order('id','desc')->column('id as value,name as label');
        }
        return CatchResponse::success($data, '提交成功', 10000);
    }


    /**
     * @return \think\response\Json
     * @date 2025/4/16-15:48
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author : 张智
     * 根据ID返回节目集信息
     */
    public function getlistbyid()
    {
        $data = Db::name('jmj')->where('id', request()->param('id'))->find();
        return CatchResponse::success($data, '提交成功', 10000);
    }


    public function generateUUID()
    {
        $data = random_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // 设置版本为4
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // 设置变体为RFC 4122
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }


    public function del()
    {


        $name = Db::name('jmj')->where('id', request()->param('id'))->value('name');


        Db::name('jm')->where('field101', $name)->delete();
        Db::name('jmj')->where('id', request()->param('id'))->delete();

        return CatchResponse::success();

    }
}